from core.rl_enviroment import RLEnv
from core.parameters import Parameters
import pandas as pd

from csv_reader import CSVReader


workers_configs = CSVReader.get_worker_configs(r'..\board_files\workers.csv')
planes_configs = CSVReader.get_plane_configs(r'..\board_files\planes.csv')
dag_dataframe = pd.read_csv(r'..\board_files\task_dag.csv')
pa = Parameters()
pa.num_nw = 5
pa.simu_len = 50
pa.num_ex = 10
pa.new_job_rate = 1
pa.backlog_size = 10
# pa.compute_dependent_parameters()
data_list_obs = []
data_list_act = []
env = RLEnv(pa, workers_configs, planes_configs,dag_dataframe)
obs = env.reset()

print(obs)
print("=====reset=======")

action_list = [1, 1, 1, 1, 5, \
               1, 1, 1, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1,1,1,0,1,1,1]

done = False

act_idx = 0

while not done:
    try:
        act = action_list[act_idx]
        # log to file
        data_list_obs.append(obs)
        data_list_act.append(act)
        obs_, r, done, info = env.step(act)
        act_idx += 1
        obs = obs_

        if act_idx % 5 == 0:
            print(env.env.now)
            print("==========")
            print(obs)

    except IndexError:
        # print(act_idx)
        print(done)
        print(obs)
        print("error")
        break



# print(env.task_backlog.backlog[-1])
print(env.env.now)
print("==========")
print(obs)
print(done)
# env.plot_gantt()


action_list = [0, 0, 1, 1, 2, \
               2, 3, 3, 4, \
               4, 1, 2, 3, 4, 0, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1, 2, 3, 4, 5, \
               0, 1,1,0,1,2,3,4,5,1,1,0,1]
obs = env.reset()
done = False

act_idx = 0
print("0000000000000000000000000000000000")
while not done:
    try:
        act = action_list[act_idx]
        obs_, r, done, info = env.step(act)
        # log to file
        data_list_obs.append(obs)
        data_list_act.append(act)
        act_idx += 1

        obs = obs_

        if act_idx % 5 == 0:
            print(env.env.now)
            print("==========")
            print(obs)
            # print(done)
            # print(act_idx)

    except IndexError:
        # print(act_idx)
        print(done)
        print(obs)
        print("error")
        break


print("==========")
print(env.env.now)
env.plot_gantt()
print(obs)
print(done)

# 生成数据集
# import numpy as np
# import pickle
#
# data_len = len(data_list_act)
#
# obs.worker.shape[0]
# woerker = np.zeros((data_len,obs.worker.shape[0],obs.worker.shape[1]))
# slot = np.zeros((data_len,obs.slot.shape[0],obs.slot.shape[1]))
# backlog = np.zeros((data_len,obs.backlog.shape[0],obs.backlog.shape[1]))
#
# for i in range(data_len):
#
#     woerker[i] = data_list_obs[i].worker
#     slot[i] = data_list_obs[i].slot
#     backlog[i] = data_list_obs[i].backlog
#
# act = np.array(data_list_act,ndmin=1)
#
# data = {'worker':woerker,\
#         'slot':slot,'backlog':backlog,\
#         'act':act
#         }
#
# pickle.dump(data,open('data.pkl','wb'))





